package com.toshiba.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.toshiba.adapter.base.BaseViewHolder;
import com.toshiba.adapter.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者: DongZhi 2015/12/24.
 * 保佑以下代码无bug...
 * RecycleView的adapter
 */
public abstract class MyRecycleViewAdapter<T> extends RecyclerView.Adapter<ViewHolder> {


    protected List<T> lists;
    protected Context context;
    private ViewGroup parentView;

    public MyRecycleViewAdapter(Context context) {
        this.context = context;
    }
    public void addList(List<T> lists) {
        if (this.lists != null && lists != null) {
            this.lists.addAll(lists);
            notifyDataSetChanged();
        }
    }

    public void clear() {
        if (this.lists != null) {
            this.lists.clear();
            notifyDataSetChanged();
        }
    }

    public List<T> getList() {
        return this.lists;
    }

    public void addItem(T items) {
        if (this.lists == null) {
            this.lists = new ArrayList<T>();
        }
        if (items != null) {
            this.lists.add(items);
            notifyDataSetChanged();
        }
    }
    //新增，移除item
    public void deleteItem(int position) {
        if (this.lists == null) {
            this.lists = new ArrayList<T>();
        }
        if (position>-1&&position<lists.size()) {
            this.lists.remove(position);
            notifyDataSetChanged();
        }
    }
    public void setList(List<T> lists) {

        if (lists != null) {
            if (this.lists == null) {
                this.lists = new ArrayList<T>();
            }
            this.lists.clear();
            this.lists.addAll(lists);
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(getItemLayout(viewType), parent,false);
        try {
            parentView = (ViewGroup) view;
        } catch (Exception e) {

        }
        return new ViewHolder(view);
    }

    public abstract void onItemInflate(int position, T item,BaseViewHolder viewHolder, View rootView);
    public abstract int getItemLayout(int viewType);

    @Override
    public void onViewRecycled(final ViewHolder holder) {
        super.onViewRecycled(holder);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        onItemInflate(position,lists.get(position),holder.getViewHolder(),holder.itemView);
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(null, v, holder.getPosition(), holder.getItemId());
                }
            });
        }
        if (onItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    onItemLongClickListener.onItemLongClick(null, holder.itemView, holder.getPosition(), holder.getItemId());
                    return true;
                }
            });
        }
        try {
            if (onWidgetListener != null ) {
                for(int i=0;i<onWidgetListener.size();i++) {
                    final int finalI = i;
                    if (onWidgetListener.get(i).onWidgetClickListener != null) {
                        holder.itemView.findViewById(onWidgetListener.get(i).id).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onWidgetListener.get(finalI).onWidgetClickListener.onWidgetClick(v, position);
                            }
                        });
                    }
                    if (onWidgetListener.get(i).onWidgetLongClickListener != null) {
                        holder.itemView.findViewById(onWidgetListener.get(i).id).setOnLongClickListener(new View.OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {
                                onWidgetListener.get(finalI).onWidgetLongClickListener.onWidgetLongClick(v, position);
                                return true;
                            }
                        });
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public int getItemCount() {
        return lists==null?0:lists.size();
    }

    private AdapterView.OnItemClickListener onItemClickListener;
    private AdapterView.OnItemLongClickListener onItemLongClickListener;
    private ArrayList<WidgetListener> onWidgetListener;


    //新增，item长按事件监听
    public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public AdapterView.OnItemLongClickListener getOnItemLongClickListener() {
        return onItemLongClickListener;
    }
    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }
    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    //新增，为item中控件添加长按事件
    public void setListenerForWiget( ArrayList<WidgetListener> onWidgetListener) {
        this.onWidgetListener = onWidgetListener;
    }

    public void myNotifyItemRemoved(int position) {
        notifyDataSetChanged();
        onWidgetListener.remove(position);
    }
}